package project.filter;

import project.entity.User;
import project.service.StudentService;
import project.service.UserService;
import project.service.impl.StudentServiceImpl;
import project.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/*
杨威
2021/9/14
14:26
*/
@WebFilter("/showAllStudentController")
public class CheckLoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //强转
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //获取session
        HttpSession session = request.getSession();
        //在session中获取user，看看是否登录了
        User user = (User) session.getAttribute("user");
        if (user != null) {//已经登录了
            filterChain.doFilter(request, response);
        } else {
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }
    }

    @Override
    public void destroy() {

    }
}
